﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class Alumini_ALM_Compose : System.Web.UI.Page
{
    RegistrationData reg = new RegistrationData();
    Communication cs = new Communication();
    DataSet ds = null;
    int UserID = 0, IsPaid = 0, UserTypeID = 0, MessageID = 0;
    string UserType = string.Empty; string recieversList = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {

            if (Request.QueryString["UserID"] == null)
                Response.Redirect(Common.InvalidPage);

            //  BindUsers(); 
        }
    }

    void BindUsers()
    {
        if (Session["User"] != null)
        {
            ds = new DataSet();
            reg = (RegistrationData)Session["User"];
            UserTypeID = reg.UserTypeID;
            UserID = reg.UserID;
            cs.UserID = UserID;
            if (Request.QueryString["SendTo"] != null)
            {
                ds = reg.GetUserID(Convert.ToInt32(Request.QueryString["SendTo"]));
                if (ds != null)
                {
                    if (ds.Tables.Count > 0)
                    {
                        if (ds.Tables[0].Rows.Count > 0)
                        {
                            txtToList.Text = Convert.ToString(ds.Tables[0].Rows[0]["Email"]);
                            ViewState["UserTo"] = Convert.ToString(ds.Tables[0].Rows[0]["UserID"]);
                        }
                    }
                }
            }

        }
    }

    protected void btnAddSelected_Click(object sender, EventArgs e)
    {
        InsertData();
        ModalPopupExtender1.Hide();
    }


    private void InsertData()
    {
        string UserTo = string.Empty, UserCc = string.Empty;

        ViewState["UserTo"] = AddUser_ToText(lstUsergroups, txtToList, UserTo);

        ViewState["UserCc"] = AddUser_ToText(lstCC, txtCc, UserCc);
    }

    string AddUser_ToText(ListView lst, TextBox txt, string List)
    {
        txt.Text = string.Empty;
        List = string.Empty;
        foreach (Control var in lst.Controls)
        {
            if (var.HasControls())
            {
                foreach (Control varInner in var.Controls)
                {
                    CheckBox ch = (CheckBox)varInner.FindControl("chkUser");
                    if (ch != null)
                    {
                        if (ch.Checked)
                        {
                            txt.Text += ch.Text + ",";
                            Label lbl = (Label)varInner.FindControl("lblUserID");
                            List += lbl.Text + ",";
                        }
                    }
                }
            }
        }


        if (txt.Text.Length > 1)
        {
            txt.Text = txt.Text.Remove(txt.Text.Trim().Length - 1, 1);
        }
        return List;
    }


    public void chkuserAll_CheckedChanged(object sender, EventArgs e)
    {
        BindCheckALL(lstUsergroups, "chkUser", sender);
        ModalPopupExtender1.Show();
    }


    public void chklstccuserAll_CheckedChanged(object sender, EventArgs e)
    {
        BindCheckALL(lstCC, "chkUser", sender);
        ModalPopupExtender1.Show();
    }


    private void BindCheckALL(ListView lvchkAll, string chkID, object sender)
    {
        CheckBox chkHeader = sender as CheckBox;
        for (int i = 0; i < lvchkAll.Items.Count; i++)
        {
            CheckBox chk = lvchkAll.Items[i].FindControl(chkID) as CheckBox;
            chk.Checked = chkHeader.Checked;
        }
    }

    protected void btnsendmsg_Click(object sender, EventArgs e)
    {
        if (Session["User"] != null)
        {
            reg = Session["User"] as RegistrationData;
            cs.HasAttachment = 0;
            UserID = reg.UserID;
            cs.SenderID = reg.UserID;
            cs.Subject = txtSubject.Text;
            SendEmails auto = new SendEmails();
            cs.Body = txtbody.Content;
            cs.IsCC = 0;
            cs.ReceiverID = Convert.ToInt32(Request.QueryString["UserID"]);
            cs.MessageID = MessageID;
            MessageID = cs.SendMessages();
            auto.SendEmail(reg.FirstName, cs.ReceiverID);
            if (MessageID > 0)
            {
                txtbody.Content = string.Empty;
                txtSubject.Text = string.Empty;
                lblMessage.Text = "Your message has been sent successfully";
            }
        }
    }

    string FileType = string.Empty, FileName = string.Empty;
    int hasAttachment()
    {
        int HasAttach = 0;
        HttpFileCollection hfc = Request.Files;
        if (hfc.Count > 0)
        {
            for (int i = 0; i < hfc.Count; i++)
            {
                HttpPostedFile hpf = hfc[i];
                if (hpf.ContentLength > 0)
                {
                    HttpPostedFile FileData = hpf;
                    long FileLength = hpf.ContentLength;
                    if (FileLength > 0)
                    {
                        //FileName = System.IO.Path.GetFileName(hpf.FileName);
                        //FileType = System.IO.Path.GetExtension(FileName).ToLower();
                        // if (FileType == ".docx" || FileType == ".doc" || FileType == ".pdf" || FileType == ".rtf" || FileType == ".txt" || FileType == ".jpg" || FileType == ".gif" || FileType == ".png" || FileType == ".bmp" || FileType == ".xls" || FileType == ".xlsx" || FileType == ".ppt" || FileType == ".pptx")
                        {
                            HasAttach = 1;
                            return HasAttach;
                        }
                        //else
                        {

                        }
                    }
                }
            }

        }
        return HasAttach;
    }

    BusinessLogic bsLogic = new BusinessLogic();
    void SaveAttachments(int MessageID)
    {
        HttpFileCollection hfc = Request.Files;
        if (hfc.Count > 0)
        {
            for (int i = 0; i < hfc.Count; i++)
            {
                HttpPostedFile hpf = hfc[i];
                if (hpf.ContentLength > 0)
                {
                    HttpPostedFile FileData = hpf;
                    long FileLength = hpf.ContentLength;
                    if (FileLength > 0)
                    {
                        FileName = System.IO.Path.GetFileName(hpf.FileName);
                        FileName = bsLogic.ValidateImageName("~/Uploads/", FileName);
                        string path = Server.MapPath("~/Uploads/") + FileName;
                        FileData.SaveAs(path);
                        cs.SaveAttachment_Path(path, FileName);
                    }
                }
            }

        }
    }

    protected void lnkOpenReviewer_Click(object sender, EventArgs e)
    {
    }



}